<?php


namespace app\common\model;

class Chatroommessage extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_chatroom_message';
    // 主键ID
    protected $pk = 'cmm_id';

	/**
	 * @param $type
	 * @param $id
	 * @param $limit
	 * @return array
	 */
	function GetHistoricalData($type,$cm_id,$id,$limit = 10){
		//判断接收端，1是用户，2是店铺
		if($type === 1){
			//是用户就查商铺信息
			$name = 'user';
		}else{
			//是商铺就查用户信息
			$name = 'shop';
		}

		$field = "crm.msg,crm.type,crm.msg_type,crm.add_time";
		$res = $this->alias('crm')
			->field($field)
			->where([
				"crm.cm_id" => $cm_id,
				"crm.{$name}_id" => $id
			])
			->order("crm.add_time desc")
			->paginate($limit);

		$this->save(["{$name}_is_unread" => 2],['cm_id'=>$cm_id]);

		return $res;
	}
}